package com.xiaolin.sports;

import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiaolin.sports.base.BaseTest;
import com.xiaolin.sports.common.enums.wechat.WechatEwmType;
import com.xiaolin.sports.model.ParentInfo;
import com.xiaolin.sports.model.SchoolInfo;
import com.xiaolin.sports.service.IParentInfoService;
import com.xiaolin.sports.service.ISchoolInfoService;
import com.xiaolin.sports.utils.DateUtil;
import com.xiaolin.sports.utils.PropertiesUtil;
import com.xiaolin.sports.utils.TextUtil;
import com.xiaolin.sports.wechat.common.Constants;
import com.xiaolin.sports.wechat.model.WechatUserInfo;
import com.xiaolin.sports.wechat.utils.WechatUtils;

public class ScanService extends BaseTest {
	
	@Autowired
	private ISchoolInfoService schoolInfoService;
	@Autowired
	private IParentInfoService parentInfoService;

	@Test
	public void aa() {
		try {
			String eventKey = "130";
			int type = Integer.parseInt(eventKey.substring(0, 1)); 
			String key = eventKey.substring(1, eventKey.length());
			String content = null;
			if(WechatEwmType.POSTER.getCode() == type) {
				Integer schoolId = Integer.valueOf(key);
				SchoolInfo schoolInfo = schoolInfoService.selectById(schoolId);
				ParentInfo parentInfo = parentInfoService.selectByOpenid("oj0oswn7sWEvJ0OgTRJBL3TJ8GoU");
				if(parentInfo != null && parentInfo.getSchoolId() != null) {
					String template = PropertiesUtil.getProperty("bind_school_had");
					String[] strings = new String[]{schoolInfo.getSchoolName(), 
							DateUtil.dateToStr(parentInfo.getBindTime(), DateUtil.DEFAULT_DATE_FORMAT),
							Constants.BASEPATH + "/course"};
					content = TextUtil.format(template, strings);
				}else {
					if(parentInfo == null) {
						parentInfo = new ParentInfo();
						WechatUserInfo userInfo = WechatUtils.getUserInfo("1PjUpSx-uwhYs7dJB-I_My-XJuCaVXu4wtFboV-eWX4-mDzQUHM1CtioFBmDQ_SZEFpRkhJ-eILPmKQYyEFbOh1EFn2YH5_jv-GhdrjfBPYSEVRylx5DMgz7jf8z_-B0CFIgAFAUXA", "oj0oswn7sWEvJ0OgTRJBL3TJ8GoU");
						parentInfo.setHeadImg(userInfo.getHeadimgurl());
						parentInfo.setCity(userInfo.getCity());
						parentInfo.setProvince(userInfo.getProvince());
						parentInfo.setNickName(userInfo.getNickname());
						parentInfo.setOpenid(userInfo.getOpenid());
					}
					parentInfo.setBindTime(new Date());
					parentInfo.setSchoolId(schoolId);
					parentInfo.setSchoolName(schoolInfo.getSchoolName());
					parentInfoService.addAndUpdate(parentInfo);
					String template = PropertiesUtil.getProperty("bind_school_success");
					String[] strings = new String[]{schoolInfo.getSchoolName(), 
							DateUtil.dateToStr(parentInfo.getBindTime(), DateUtil.DEFAULT_DATE_FORMAT),
							Constants.BASEPATH + "/user/account"};
					content = TextUtil.format(template, strings);
				}
			}
			System.out.println(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
